from itertools import accumulate
import json
from openpyxl import load_workbook


def main():
    wb = load_workbook('仪器清单.xlsx')
    instruments_sheet = wb.active
    inner_manage_ids, inner_manage_names = instruments_sheet['D:E']
    manage_ids = [cell.value for cell in inner_manage_ids[2:]]
    manage_names = [cell.value for cell in inner_manage_names[2:]]
    manage_infos_pairs = []
    # 去重
    key_set = set()
    for manage_id, manage_name in zip(manage_ids, manage_names):
        # 只要 id 和 name 有一个不为空，就认为该对数据有效
        if manage_id is not None or manage_name is not None:
            if manage_id is None:
                manage_id = '/'
            if manage_name is None:
                manage_name = '/'
            key = manage_id.strip() + manage_name.strip()
            if key not in key_set:
                key_set.add(key)
                manage_infos_pairs.append(
                    (manage_id.strip(), manage_name.strip()))
            else:
                print(manage_id, manage_name)

    # 将 manage_infos_pairs 改写成 json 格式
    manage_infos_json = []
    for manage_id, manage_name in manage_infos_pairs:
        manage_infos_json.append({'id': manage_id, 'name': manage_name})

    instruments = {'instruments': manage_infos_json}

    # 将 instruments 写入文件
    with open('instruments.json', 'w', encoding='utf-8') as f:
        json.dump(instruments, f, ensure_ascii=False, separators=(',', ':'))


if __name__ == '__main__':
    main()
